//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.wi.agency;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.util.ResourceBundle;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.iconBar.IconBar;
import org.jpgolay.wi.util.ImageFilter;
import org.jpgolay.wi.util.ImagePreview;
import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

public class AgencyGUI extends JInternalFrame {

    private class TableModel extends AbstractTableModel {

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Class getColumnClass(int col) {
            return types[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public boolean isCellEditable(int row, int col) {
            if (col <= 1) {
                return false;
            } else {
                return cellEditable;
            }
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
        Class types[];

        TableModel() {
            types = (new Class[]{
                        java.lang.Object.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
                    });
        }
    }

    public AgencyGUI() {
        cellEditable = false;
        initComponents();
    }

    private void initComponents() {
        iconBar1 = new IconBar();
        accessTP = new JTabbedPane();
        addressPhoneP = new JPanel();
        addressPhoneFieldP = new JPanel();
        statusL = new JLabel();
        status = new JTextField();
        compagnyL = new JLabel();
        compagny = new JTextField();
        addrLn1L = new JLabel();
        addrLn1 = new JTextField();
        addrLn2L = new JLabel();
        addrLn2 = new JTextField();
        cityL = new JLabel();
        city = new JTextField();
        countyL = new JLabel();
        county = new JTextField();
        zipL = new JLabel();
        zip = new JTextField();
        tel1L = new JLabel();
        tel1 = new JTextField();
        tel2L = new JLabel();
        tel2 = new JTextField();
        faxL = new JLabel();
        fax = new JTextField();
        emailL = new JLabel();
        email = new JTextField();
        webL = new JLabel();
        web = new JTextField();
        logoL = new JLabel();
        compagnyNaf = new JTextField();
        compagnySiretL = new JLabel();
        compagnySiret = new JTextField();
        compagnyNafL = new JLabel();
        accessP = new JPanel();
        accessFieldP = new JPanel();
        idL = new JLabel();
        id = new JLabel();
        groupId = new JComboBox();
        usernameL = new JLabel();
        username = new JTextField();
        passL = new JLabel();
        pass = new JTextField();
        paymentDueL = new JLabel();
        paymentDue = new JTextField();
        commentL = new JLabel();
        comment = new JTextField();
        expiryDateL = new JLabel();
        expiryDate = new DateComboBox();
        accessTableP = new JPanel();
        accessTableSP = new JScrollPane();
        accessTable = new JTable();
        sendEmailP = new JPanel();
        fieldSendEmailP = new JPanel();
        welcomeTitleL = new JLabel();
        welcomeTitle = new JTextField();
        welcomeEmailSP = new JScrollPane();
        welcomeEmail = new JTextArea();
        newAdTitleL = new JLabel();
        newAdTitle = new JTextField();
        newAdEmailUpSP = new JScrollPane();
        newAdEmailUp = new JTextArea();
        newAdEmailDownSP = new JScrollPane();
        newAdEmailDown = new JTextArea();
        smtpL = new JLabel();
        smtp = new JTextField();
        wwwL = new JLabel();
        www = new JTextField();
        bottomP = new JPanel();
        lastUser = new JLabel(" ", 2);
        lastUpdate = new JLabel(" ", 4);
        jMenuBar1 = new JMenuBar();
        optionsM = new JMenu();
        newMI = new JMenuItem();
        openMI = new JMenuItem();
        saveMI = new JMenuItem();
        modifyMI = new JMenuItem();
        removeMI = new JMenuItem();
        jSeparator1 = new JSeparator();
        printMI = new JMenuItem();
        helpMI = new JMenuItem();
        setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/logo.gif")));
        setTitle(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("agency_title"));
        iconBar1.setEnabledPrint(false);
        iconBar1.setEnabledCreate(false);
        iconBar1.setEnabledModify(false);
        iconBar1.setEnabledRemove(false);
        iconBar1.setEnabledSave(false);
        getContentPane().add(iconBar1, "North");
        addressPhoneFieldP.setLayout(new AbsoluteLayout());
        statusL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("status"));
        statusL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(statusL, new AbsoluteConstraints(0, 25, 100, 23));
        addressPhoneFieldP.add(status, new AbsoluteConstraints(100, 25, 150, 23));
        compagnyL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("compagny"));
        compagnyL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(compagnyL, new AbsoluteConstraints(0, 0, 100, 23));
        addressPhoneFieldP.add(compagny, new AbsoluteConstraints(100, 0, 150, 23));
        addrLn1L.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Address"));
        addrLn1L.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(addrLn1L, new AbsoluteConstraints(0, 50, 100, 23));
        addressPhoneFieldP.add(addrLn1, new AbsoluteConstraints(100, 50, 150, 23));
        addrLn2L.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Address"));
        addrLn2L.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(addrLn2L, new AbsoluteConstraints(0, 75, 100, 23));
        addressPhoneFieldP.add(addrLn2, new AbsoluteConstraints(100, 75, 150, 23));
        cityL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("City"));
        cityL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(cityL, new AbsoluteConstraints(0, 125, 100, 23));
        addressPhoneFieldP.add(city, new AbsoluteConstraints(100, 125, 100, 23));
        countyL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("County"));
        countyL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(countyL, new AbsoluteConstraints(0, 100, 100, 23));
        addressPhoneFieldP.add(county, new AbsoluteConstraints(100, 100, 150, 23));
        zipL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Zip"));
        zipL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(zipL, new AbsoluteConstraints(210, 125, 60, 23));
        addressPhoneFieldP.add(zip, new AbsoluteConstraints(280, 125, 100, 23));
        tel1L.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Tel_1"));
        tel1L.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(tel1L, new AbsoluteConstraints(0, 150, 100, 23));
        addressPhoneFieldP.add(tel1, new AbsoluteConstraints(100, 150, 100, 23));
        tel2L.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Tel_2"));
        tel2L.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(tel2L, new AbsoluteConstraints(210, 150, 60, 23));
        addressPhoneFieldP.add(tel2, new AbsoluteConstraints(280, 150, 100, 23));
        faxL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Fax"));
        faxL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(faxL, new AbsoluteConstraints(0, 175, 100, 23));
        addressPhoneFieldP.add(fax, new AbsoluteConstraints(100, 175, 100, 23));
        emailL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Email"));
        emailL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(emailL, new AbsoluteConstraints(0, 200, 100, 23));
        addressPhoneFieldP.add(email, new AbsoluteConstraints(100, 200, 280, 23));
        webL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Web_site"));
        webL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(webL, new AbsoluteConstraints(210, 175, 60, -1));
        addressPhoneFieldP.add(web, new AbsoluteConstraints(280, 175, 100, 23));
        logoL.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("logo"));
        logoL.setBorder(new EtchedBorder(0));
        logoL.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                logoLAction(e);
            }
        });
        addressPhoneFieldP.add(logoL, new AbsoluteConstraints(260, 0, 120, 120));
        addressPhoneFieldP.add(compagnyNaf, new AbsoluteConstraints(280, 225, 100, 23));
        compagnySiretL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("compagny_siret"));
        compagnySiretL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        addressPhoneFieldP.add(compagnySiretL, new AbsoluteConstraints(0, 225, 100, 23));
        addressPhoneFieldP.add(compagnySiret, new AbsoluteConstraints(100, 225, 100, 23));
        compagnyNafL.setHorizontalAlignment(0);
        compagnyNafL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("compagny_naf"));
        addressPhoneFieldP.add(compagnyNafL, new AbsoluteConstraints(200, 225, 80, 23));
        addressPhoneP.add(addressPhoneFieldP);
        accessTP.addTab(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Address_and_Phone"), new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/vert.png")), addressPhoneP);
        accessFieldP.setLayout(new AbsoluteLayout());
        idL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("ID"));
        idL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(idL, new AbsoluteConstraints(0, 0, 50, 23));
        accessFieldP.add(id, new AbsoluteConstraints(50, 0, 50, 25));
        groupId.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Group_ID"));
        accessFieldP.add(groupId, new AbsoluteConstraints(100, 0, 280, 23));
        usernameL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Username"));
        usernameL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(usernameL, new AbsoluteConstraints(0, 140, 90, 23));
        accessFieldP.add(username, new AbsoluteConstraints(90, 140, 100, 23));
        passL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Password"));
        passL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(passL, new AbsoluteConstraints(190, 140, 90, 23));
        accessFieldP.add(pass, new AbsoluteConstraints(280, 140, 100, 23));
        paymentDueL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Payment_due"));
        paymentDueL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(paymentDueL, new AbsoluteConstraints(0, 165, 90, 23));
        accessFieldP.add(paymentDue, new AbsoluteConstraints(90, 165, 100, 23));
        commentL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Comment"));
        commentL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(commentL, new AbsoluteConstraints(0, 190, 90, 23));
        accessFieldP.add(comment, new AbsoluteConstraints(90, 190, 290, 23));
        expiryDateL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Expiry_Date"));
        expiryDateL.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
        accessFieldP.add(expiryDateL, new AbsoluteConstraints(190, 165, 90, 23));
        expiryDate.setToolTipText("jj/mm/aaaa");
        accessFieldP.add(expiryDate, new AbsoluteConstraints(280, 165, 100, 23));
        accessTableP.setLayout(new BorderLayout());
        accessTableP.setBorder(new EtchedBorder(0));
        accessTableSP.setHorizontalScrollBarPolicy(32);
        accessTable.setModel(new TableModel());
        accessTable.getColumnModel().getColumn(0).setPreferredWidth(20);
        accessTable.getColumnModel().getColumn(1).setPreferredWidth(100);
        accessTable.getColumnModel().getColumn(2).setPreferredWidth(100);
        accessTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        accessTableSP.setViewportView(accessTable);
        accessTableP.add(accessTableSP, "Center");
        accessFieldP.add(accessTableP, new AbsoluteConstraints(0, 25, 380, 110));
        accessP.add(accessFieldP);
        accessTP.addTab(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Access"), new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/bleu.png")), accessP);
        fieldSendEmailP.setLayout(new AbsoluteLayout());
        welcomeTitleL.setHorizontalAlignment(0);
        welcomeTitleL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("welcome_agency"));
        fieldSendEmailP.add(welcomeTitleL, new AbsoluteConstraints(0, 0, 100, 23));
        fieldSendEmailP.add(welcomeTitle, new AbsoluteConstraints(100, 0, 280, 23));
        welcomeEmail.setLineWrap(true);
        welcomeEmail.setRows(4);
        welcomeEmail.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("welcom_email"));
        welcomeEmail.setWrapStyleWord(true);
        welcomeEmailSP.setViewportView(welcomeEmail);
        fieldSendEmailP.add(welcomeEmailSP, new AbsoluteConstraints(0, 24, 380, 46));
        newAdTitleL.setHorizontalAlignment(0);
        newAdTitleL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("newAd_title"));
        fieldSendEmailP.add(newAdTitleL, new AbsoluteConstraints(0, 90, 100, 23));
        fieldSendEmailP.add(newAdTitle, new AbsoluteConstraints(100, 90, 280, 23));
        newAdEmailUp.setLineWrap(true);
        newAdEmailUp.setRows(4);
        newAdEmailUp.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("new_ad_email_up"));
        newAdEmailUp.setWrapStyleWord(true);
        newAdEmailUpSP.setViewportView(newAdEmailUp);
        fieldSendEmailP.add(newAdEmailUpSP, new AbsoluteConstraints(0, 114, 380, 46));
        newAdEmailDown.setLineWrap(true);
        newAdEmailDown.setRows(4);
        newAdEmailDown.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("new_ad_email_down"));
        newAdEmailDown.setWrapStyleWord(true);
        newAdEmailDownSP.setViewportView(newAdEmailDown);
        fieldSendEmailP.add(newAdEmailDownSP, new AbsoluteConstraints(0, 161, 380, 46));
        smtpL.setHorizontalAlignment(0);
        smtpL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("smtp"));
        fieldSendEmailP.add(smtpL, new AbsoluteConstraints(0, 220, 60, 23));
        fieldSendEmailP.add(smtp, new AbsoluteConstraints(60, 220, 100, 23));
        wwwL.setHorizontalAlignment(0);
        wwwL.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("www"));
        fieldSendEmailP.add(wwwL, new AbsoluteConstraints(160, 220, 60, 23));
        fieldSendEmailP.add(www, new AbsoluteConstraints(220, 220, 160, 23));
        sendEmailP.add(fieldSendEmailP);
        accessTP.addTab(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Send_Email"), new ImageIcon(getClass().getResource("/org/jpgolay/wi/images/jaune.png")), sendEmailP);
        getContentPane().add(accessTP, "Center");
        bottomP.setLayout(new GridLayout(1, 0));
        lastUser.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Last_User"));
        bottomP.add(lastUser);
        lastUpdate.setToolTipText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Last_Update"));
        bottomP.add(lastUpdate);
        getContentPane().add(bottomP, "South");
        optionsM.setMnemonic('O');
        optionsM.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("Options"));
        newMI.setAccelerator(KeyStroke.getKeyStroke(78, 2));
        newMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/new.png")));
        newMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("new"));
        newMI.setEnabled(false);
        optionsM.add(newMI);
        openMI.setAccelerator(KeyStroke.getKeyStroke(79, 2));
        openMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/open.png")));
        openMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("open"));
        optionsM.add(openMI);
        saveMI.setAccelerator(KeyStroke.getKeyStroke(83, 2));
        saveMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/save.png")));
        saveMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("save"));
        saveMI.setEnabled(false);
        optionsM.add(saveMI);
        modifyMI.setAccelerator(KeyStroke.getKeyStroke(77, 2));
        modifyMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/modify.png")));
        modifyMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("modify"));
        modifyMI.setEnabled(false);
        optionsM.add(modifyMI);
        removeMI.setAccelerator(KeyStroke.getKeyStroke(88, 2));
        removeMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/delete.png")));
        removeMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("delete"));
        removeMI.setEnabled(false);
        optionsM.add(removeMI);
        optionsM.add(jSeparator1);
        printMI.setAccelerator(KeyStroke.getKeyStroke(80, 2));
        printMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/printer.png")));
        printMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("print"));
        printMI.setEnabled(false);
        optionsM.add(printMI);
        helpMI.setAccelerator(KeyStroke.getKeyStroke(65, 2));
        helpMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/images/help.png")));
        helpMI.setText(ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("help"));
        optionsM.add(helpMI);
        jMenuBar1.add(optionsM);
        setJMenuBar(jMenuBar1);
        pack();
    }

    private void logoLAction(MouseEvent e) {
        if (username.isEnabled()) {
            if (e.getButton() == 1) {
                if (photoFileChooser()) {
                    if ((logo = loadImage(selectedFile.getAbsolutePath())) == null) {
                        JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("max_picrure_size"));
                    } else {
                        logoL.setIcon(resizeImageIcon(new ImageIcon(logo)));
                    }
                } else {
                    JOptionPane.showMessageDialog(this, ResourceBundle.getBundle("org/jpgolay/wi/wii").getString("dont_load_selected_picture"));
                }
            } else if (e.getButton() == 2) {
                logo = null;
                logoL.setIcon(null);
            }
        }
    }

    private ImageIcon resizeImageIcon(ImageIcon tmpIcon) {
        if (tmpIcon.getIconWidth() > logoL.getWidth()) {
            return new ImageIcon(tmpIcon.getImage().getScaledInstance(logoL.getWidth(), -1, 1));
        } else {
            return tmpIcon;
        }
    }

    private boolean photoFileChooser() {
        if (fileChooser == null) {
            fileChooser = new JFileChooser();
            fileChooser.addChoosableFileFilter(new ImageFilter());
            fileChooser.setAcceptAllFileFilterUsed(false);
            fileChooser.setAccessory(new ImagePreview(fileChooser));
        }
        int result = fileChooser.showOpenDialog(this);
        if (result == 0) {
            selectedFile = fileChooser.getSelectedFile();
            return true;
        } else {
            return false;
        }
    }

    private byte[] loadImage(String path) {
        try {
            File f = new File(path);
            int len = (int) f.length();
            byte b[] = new byte[len];
            FileInputStream fin = new FileInputStream(f);
            for (int offset = 0; offset < len; offset += fin.read(b, offset, len - offset));
            fin.close();
            if (b.length > 55000) {
                b = (byte[]) null;
            }
            return b;
        } catch (IOException e) {
            return null;
        }
    }

    public String getId() {
        return id.getText();
    }

    public void setId(String id) {
        this.id.setText(id);
    }

    public String getGroupId() {
        return ((String) groupId.getSelectedItem()).substring(0, 1);
    }

    public void setGroupId(int index) {
        groupId.setSelectedIndex(index);
    }

    public void setAllGroupId(String groupId[]) {
        this.groupId.setModel(new DefaultComboBoxModel(groupId));
    }

    public String getUsername() {
        return username.getText();
    }

    public void setUsername(String username) {
        this.username.setText(username);
    }

    public String getPass() {
        return pass.getText();
    }

    public void setPass(String pass) {
        this.pass.setText(pass);
    }

    public String getCompagny() {
        return compagny.getText();
    }

    public void setCompagny(String compagny) {
        this.compagny.setText(compagny);
    }

    public String getStatus() {
        return status.getText();
    }

    public void setStatus(String status) {
        this.status.setText(status);
    }

    public String getAddrLn1() {
        return addrLn1.getText();
    }

    public void setAddrLn1(String addrLn1) {
        this.addrLn1.setText(addrLn1);
    }

    public String getAddrLn2() {
        return addrLn2.getText();
    }

    public void setAddrLn2(String addrLn2) {
        this.addrLn2.setText(addrLn2);
    }

    public String getCity() {
        return city.getText();
    }

    public void setCity(String city) {
        this.city.setText(city);
    }

    public String getCounty() {
        return county.getText();
    }

    public void setCounty(String county) {
        this.county.setText(county);
    }

    public String getZip() {
        return zip.getText();
    }

    public void setZip(String zip) {
        this.zip.setText(zip);
    }

    public String getTel1() {
        return tel1.getText();
    }

    public void setTel1(String tel1) {
        this.tel1.setText(tel1);
    }

    public String getTel2() {
        return tel2.getText();
    }

    public void setTel2(String tel2) {
        this.tel2.setText(tel2);
    }

    public String getFax() {
        return fax.getText();
    }

    public void setFax(String fax) {
        this.fax.setText(fax);
    }

    public String getEmail() {
        return email.getText();
    }

    public void setEmail(String email) {
        this.email.setText(email);
    }

    public String getWeb() {
        return web.getText();
    }

    public void setWeb(String web) {
        this.web.setText(web);
    }

    public int getAccess() {
        return getAccessAgency();
    }

    public void setAccess(int access) {
        showAccessAgency(access);
    }

    public String getExpiryDate() {
        return (String) expiryDate.getSelectedItem();
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate.setModel(new DefaultComboBoxModel(new String[]{
                    expiryDate
                }));
    }

    public String getPaymentDue() {
        return paymentDue.getText();
    }

    public void setPaymentDue(String paymentDue) {
        this.paymentDue.setText(paymentDue);
    }

    public String getComment() {
        return comment.getText();
    }

    public void setComment(String comment) {
        this.comment.setText(comment);
    }

    public Icon getLogoIcon() {
        return logoL.getIcon();
    }

    public void setLogo(byte logo[]) {
        this.logo = logo;
        if (logo != null) {
            logoL.setIcon(resizeImageIcon(new ImageIcon(logo)));
        }
    }

    public byte[] getLogo() {
        return logo;
    }

    public String getLastUpdate() {
        return lastUpdate.getText();
    }

    public void setLastUpdate(String lastUpdate) {
        this.lastUpdate.setText(lastUpdate);
    }

    public String getLastUser() {
        return lastUser.getText();
    }

    public void setLastUser(String lastUser) {
        this.lastUser.setText(lastUser);
    }

    public JMenuItem getHelpMI() {
        return helpMI;
    }

    public IconBar getIconBar1() {
        return iconBar1;
    }

    public JMenuItem getModifyMI() {
        return modifyMI;
    }

    public JMenuItem getNewMI() {
        return newMI;
    }

    public JMenuItem getOpenMI() {
        return openMI;
    }

    public JMenuItem getPrintMI() {
        return printMI;
    }

    public JMenuItem getSaveMI() {
        return saveMI;
    }

    public JMenuItem getRemoveMI() {
        return removeMI;
    }

    public String getCompagnyNaf() {
        return compagnyNaf.getText();
    }

    public String getCompagnySiret() {
        return compagnySiret.getText();
    }

    public String getNewAdEmailDown() {
        return newAdEmailDown.getText();
    }

    public String getNewAdEmailUp() {
        return newAdEmailUp.getText();
    }

    public String getNewAdTitle() {
        return newAdTitle.getText();
    }

    public String getSmtp() {
        return smtp.getText();
    }

    public String getWelcomeEmail() {
        return welcomeEmail.getText();
    }

    public String getWelcomeTitle() {
        return welcomeTitle.getText();
    }

    public String getWww() {
        return www.getText();
    }

    public void setCompagnyNaf(String compagnyNaf) {
        this.compagnyNaf.setText(compagnyNaf);
    }

    public void setCompagnySiret(String compagnySiret) {
        this.compagnySiret.setText(compagnySiret);
    }

    public void setNewAdEmailDown(String newAdEmailDown) {
        this.newAdEmailDown.setText(newAdEmailDown);
    }

    public void setNewAdEmailUp(String newAdEmailUp) {
        this.newAdEmailUp.setText(newAdEmailUp);
    }

    public void setNewAdTitle(String newAdTitle) {
        this.newAdTitle.setText(newAdTitle);
    }

    public void setSmtp(String smtp) {
        this.smtp.setText(smtp);
    }

    public void setWelcomeEmail(String welcomeEmail) {
        this.welcomeEmail.setText(welcomeEmail);
    }

    public void setWelcomeTitle(String welcomeTitle) {
        this.welcomeTitle.setText(welcomeTitle);
    }

    public void setWww(String www) {
        this.www.setText(www);
    }

    public void initFields() {
        setId("");
        setUsername("");
        setPass("");
        setCompagny("");
        setStatus("");
        setAddrLn1("");
        setAddrLn2("");
        setCity("");
        setCounty("");
        setZip("");
        setTel1("");
        setTel2("");
        setFax("");
        setEmail("");
        setWeb("");
        initAccessTable();
        setExpiryDate("");
        setPaymentDue("");
        setComment("");
        logoL.setIcon(null);
        setLastUpdate("");
        setLastUser("");
        setCompagnyNaf("");
        setCompagnySiret("");
        setNewAdEmailDown("");
        setNewAdEmailUp("");
        setNewAdTitle("");
        setSmtp("");
        setWelcomeEmail("");
        setWelcomeTitle("");
        setWww("");
    }

    public void editableFields(boolean b) {
        groupId.setEnabled(b);
        username.setEnabled(b);
        pass.setEnabled(b);
        compagny.setEnabled(b);
        status.setEnabled(b);
        addrLn1.setEnabled(b);
        addrLn2.setEnabled(b);
        city.setEnabled(b);
        county.setEnabled(b);
        zip.setEnabled(b);
        tel1.setEnabled(b);
        tel2.setEnabled(b);
        fax.setEnabled(b);
        email.setEnabled(b);
        web.setEnabled(b);
        expiryDate.setEnabled(b);
        paymentDue.setEnabled(b);
        comment.setEnabled(b);
        compagnyNaf.setEnabled(b);
        compagnySiret.setEnabled(b);
        newAdEmailDown.setEnabled(b);
        newAdEmailUp.setEnabled(b);
        newAdTitle.setEnabled(b);
        smtp.setEnabled(b);
        welcomeEmail.setEnabled(b);
        welcomeTitle.setEnabled(b);
        www.setEnabled(b);
        cellEditable = b;
    }

    private void initAccessTable() {
        int row = accessTable.getRowCount();
        for (int i = 0; i < row; i++) {
            accessTable.setValueAt(new Boolean(false), i, 2);
            accessTable.setValueAt(new Boolean(false), i, 3);
        }

        accessTableSP.setViewportView(accessTable);
    }

    private int getAccessAgency() {
        int nRow = accessTable.getRowCount();
        if (nRow < 17) {
            int a = 0;
            int code = 0;
            for (int i = 0; i < nRow; i++) {
                if (((Boolean) accessTable.getValueAt(i, 2)).booleanValue()) {
                    a += userAcces[code];
                }
                if (((Boolean) accessTable.getValueAt(i, 3)).booleanValue()) {
                    a += userAcces[code + 1];
                }
                code += 2;
            }

            return a;
        } else {
            return 0;
        }
    }

    private void showAccessAgency(int a) {
        String b = Long.toBinaryString(a);
        int position = b.length();
        int row = 0;
        int col = 2;
        while (position > 0) {
            if (--position >= 0 && b.charAt(position) == '1') {
                accessTable.setValueAt(new Boolean(true), row, col);
            } else {
                accessTable.setValueAt(new Boolean(false), row, col);
            }
            if (col == 2) {
                col = 3;
            } else {
                col = 2;
                row++;
            }
        }
        accessTableSP.setViewportView(accessTable);
    }
    private JPanel accessFieldP;
    private JPanel accessP;
    private JTabbedPane accessTP;
    private JTable accessTable;
    private JPanel accessTableP;
    private JScrollPane accessTableSP;
    private JTextField addrLn1;
    private JLabel addrLn1L;
    private JTextField addrLn2;
    private JLabel addrLn2L;
    private JPanel addressPhoneFieldP;
    private JPanel addressPhoneP;
    private JPanel bottomP;
    private JTextField city;
    private JLabel cityL;
    private JTextField comment;
    private JLabel commentL;
    private JTextField compagny;
    private JLabel compagnyL;
    private JTextField compagnyNaf;
    private JLabel compagnyNafL;
    private JTextField compagnySiret;
    private JLabel compagnySiretL;
    private JTextField county;
    private JLabel countyL;
    private JTextField email;
    private JLabel emailL;
    private DateComboBox expiryDate;
    private JLabel expiryDateL;
    private JTextField fax;
    private JLabel faxL;
    private JPanel fieldSendEmailP;
    private JComboBox groupId;
    private JMenuItem helpMI;
    private IconBar iconBar1;
    private JLabel id;
    private JLabel idL;
    private JMenuBar jMenuBar1;
    private JSeparator jSeparator1;
    private JLabel lastUpdate;
    private JLabel lastUser;
    private JLabel logoL;
    private JMenuItem modifyMI;
    private JTextArea newAdEmailDown;
    private JScrollPane newAdEmailDownSP;
    private JTextArea newAdEmailUp;
    private JScrollPane newAdEmailUpSP;
    private JTextField newAdTitle;
    private JLabel newAdTitleL;
    private JMenuItem newMI;
    private JMenuItem openMI;
    private JMenu optionsM;
    private JTextField pass;
    private JLabel passL;
    private JTextField paymentDue;
    private JLabel paymentDueL;
    private JMenuItem printMI;
    private JMenuItem removeMI;
    private JMenuItem saveMI;
    private JPanel sendEmailP;
    private JTextField smtp;
    private JLabel smtpL;
    private JTextField status;
    private JLabel statusL;
    private JTextField tel1;
    private JLabel tel1L;
    private JTextField tel2;
    private JLabel tel2L;
    private JTextField username;
    private JLabel usernameL;
    private JTextField web;
    private JLabel webL;
    private JTextArea welcomeEmail;
    private JScrollPane welcomeEmailSP;
    private JTextField welcomeTitle;
    private JLabel welcomeTitleL;
    private JTextField www;
    private JLabel wwwL;
    private JTextField zip;
    private JLabel zipL;
    private byte logo[];
    private JFileChooser fileChooser;
    private File selectedFile;
    private Object data[][] = {
        {
            "1", "Site N\2601", new Boolean(false), new Boolean(false)
        }, {
            "2", "Site N\2602", new Boolean(false), new Boolean(false)
        }, {
            "3", "Site N\2603", new Boolean(false), new Boolean(false)
        }, {
            "4", "Site N\2604", new Boolean(false), new Boolean(false)
        }, {
            "5", "Site N\2605", new Boolean(false), new Boolean(false)
        }, {
            "6", "Site N\2606", new Boolean(false), new Boolean(false)
        }, {
            "7", "Site N\2607", new Boolean(false), new Boolean(false)
        }
    };
    private String columnNames[] = {
        "", "Synchronisation", "Lecture", "Ecriture"
    };
    private boolean cellEditable;
    private int userAcces[] = {
        1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
        1024, 2048, 4096, 8192, 16384, 32768, 0x10000, 0x20000, 0x40000, 0x80000,
        0x100000, 0x200000, 0x400000, 0x800000, 0x1000000, 0x2000000, 0x4000000, 0x8000000, 0x10000000, 0x20000000,
        0x40000000
    };
}
